package com.fingerchar.core.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 接口公共响应实体
 *
 * @Title：calCore
 * @Description：
 * @Author：zhangly
 * @Date：2019/5/13 14:36
 * @Version：1.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)

public class ResponseBean<T> {

    private int code;
    private String message;
    private T data;
    private long count;
    private boolean success;

    public static <T> ResponseBean<T> success() {
        return success(null, 0, "");
    }

    public static <T> ResponseBean<T> success(T data) {
        return success(data, 0, "");
    }

    public static <T> ResponseBean<T> success(T data, long count) {
        return success(data, count, "");
    }

    public static <T> ResponseBean<T> success(T data, String message) {
        return success(data, 0, message);
    }

    public static <T> ResponseBean<T> success(T data, long count, String message) {
        return new ResponseBean(200, message, data, count, true);
    }

    public static <T> ResponseBean<T> error(int code, String message, T data) {
        return new ResponseBean(code, message, data, 0, false);
    }

    public static <T> ResponseBean<T> error(int code, String message) {
        return error(code, message, null);
    }

    public static <T> ResponseBean<T> error(String message) {
        return error(400, message, null);
    }

    public static <T> ResponseBean<T> error(String message, T data) {
        return error(400, message, data);
    }

    public static <T> ResponseBean<T> of(boolean success, String message, T data) {
        return success ? success(data, message) : error(message, data);
    }

    public static <T> ResponseBean<T> of(boolean success, String message) {
        return success ? success(null, message) : error(message);
    }

    public static <T> ResponseBean<T> of(boolean success) {
        return of(success, "");
    }
}
